var baseFn={
    getQueryString:function(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  decodeURI(r[2]); return null;
    }
}
window.onload=function(){
    var queryData={
        'booking_id':baseFn.getQueryString('booking_id') == null?1:baseFn.getQueryString('booking_id'),
        'user_id' :baseFn.getQueryString('user_id') == null?1:baseFn.getQueryString('user_id'),
        'patient_id':baseFn.getQueryString('patient_id') == null?1:baseFn.getQueryString('patient_id'),
        'doctor_id':baseFn.getQueryString('doctor_id') == null?1:baseFn.getQueryString('doctor_id'),
        'rel_id':baseFn.getQueryString('rel_id') == null?1:baseFn.getQueryString('rel_id'),
        'treatment_id':baseFn.getQueryString('rel_id') == null?1:baseFn.getQueryString('treatment_id'),
        answer:[],
    }
    var url = {
        base:'http://wechat.xrtn.dmpp.net.cn/children/frontend/web/index.php?r=child/',
        post:'answer',
        getCnt:'getquestion'
    }
    var isrq2=false
    if($('body').hasClass('rqu2')){
        isrq2= true
        url.post=''
        url.getCnt='getdisease'
        
    }
    var listvue = new Vue({
        el:'.listvue',
        data:{
            info:[
                {
                    id:'0',
                    tit:'母亲孕期饮食',
                    qlist:[
                        {id:'0',txt:'正常',sel:false},
                        {id:'1',txt:'有一点偏食水果/辛辣/肉类',sel:false},
                        {id:'2',txt:'有一些偏食水果/辛辣/肉类',sel:false},
                        {id:'3',txt:'非常偏食水果/辛辣/肉类',sel:false},
                        {id:'4',txt:'大量水果/辛辣/肉类',sel:false},
                        {id:'5',txt:'极端水果/辛辣/肉类',sel:false}
                    ]			  

                },
                {
                    id:'1',
                    tit:'母亲怀孕生产',
                    qlist:[
                        {id:'0',txt:'适龄足月顺产/剖腹',sel:false},
                        {id:'1',txt:'高龄足月顺产/剖腹',sel:false},
                        {id:'2',txt:'适龄/高龄剖腹早产7天之内',sel:false},
                        {id:'3',txt:'适龄/高龄剖腹早产7-14天',sel:false},
                        {id:'4',txt:'适龄/高龄剖腹早产14-21天',sel:false},
                        {id:'5',txt:'适龄/高龄剖腹早产21天到 天',sel:false}
                    ]
                },
                {
                    id:'2',
                    tit:'家族病史',
                    qlist:[
                        {id:'0',txt:'无',sel:false},
                        {id:'1',txt:'有1种病史（手写）',sel:false},
                        {id:'2',txt:'有2种病史（手写）',sel:false},
                        {id:'3',txt:'有3种病史（手写）',sel:false},
                        {id:'4',txt:'有4种病史（手写）',sel:false},
                        {id:'5',txt:'有5种病史（手写）',sel:false}
                    ],
                    hasT:true,
                    tCnt:''
                },
                {		
                    id:'3',								
                    tit:'参加体育运动频率，如游泳/打篮球/踢足球/上体育课/跑步/跳舞/跳绳/跆拳道/轮滑/体能训练',
                    qlist:[
                        {id:'0',txt:'正常',sel:false},
                        {id:'1',txt:'偶尔参加，可以控制不参加',sel:false},
                        {id:'2',txt:'有时参加',sel:false},
                        {id:'3',txt:'经常参加',sel:false},
                        {id:'4',txt:'总是参加',sel:false},
                        {id:'5',txt:'生病也必须参加',sel:false}
                    ]
                }
            ]
        },
        computed:{
            'booking_time':function(){
                if(this.time == ''){
                    return this.date
                }else{
                    return this.date+' '+this.time+':00'
                }
            }
        },
        methods:{
            selRadio:function(ids,ids2){
                // console.log(ids,ids2)
                for(var i = 0;i<this.info[ids].qlist.length;i++){
                    if(i != ids2){
                        this.info[ids].qlist[i].sel = 'false'
                    }
                }
                this.info[ids].qlist[ids2].sel = 'true'
            },
            selRadio2:function(ids,ids2){
                var _qList = 'qlist'
                if(isrq2){
                    _qList = 'qlist'
                }
                // console.log(ids,ids2)
                for(var i = 0;i<this.info[ids][_qList].length;i++){
                    if(i != ids2){
                        this.info[ids][_qList][i].sel = 'false'
                    }
                }
                this.info[ids][_qList][ids2].sel = 'true'
            },
            check:function(){                
                var _upDatas={
                    'booking_id':queryData.booking_id,
                    'user_id' :queryData.user_id,
                    'patient_id':queryData.patient_id,
                    'doctor_id':queryData.doctor_id,
                    'rel_id':queryData.rel_id,
                    answer:[]
                }
                var _tempData = this.info
                for(var i =0;i<_tempData.length;i++){
                    var _answer = {
                        type_id:_tempData[i].id
                    }
                    // var _q = _tempData[i].qlist
                    var _selQ = _tempData[i].qlist.filter(function(val){
                        return val.sel == 'true'
                    })
                    if(_selQ.length == 1){
                        _answer['nursing_id'] = _selQ[0].id                       
                        _answer['nursing_name'] = _selQ[0].txt                       
                        _answer['nursing_value'] = _selQ[0].nursing_value                       
                        _answer['type_name'] = _tempData[i].tit                      
                        _upDatas.answer.push(_answer)
                    }else{
                        popShow({
                            txt:'您还有没选中的题目！'
                        })
                        return false
                    }
                    
                }
                //console.log(_upDatas)
                // return false;
                $.post(url.base+url.post,_upDatas,function(res){
                    // alert(res.info)
                    popShow({
                        txt:res.info
                    })
                },'json')
            },
            check2:function(){                
                var _upDatas={
                    'booking_id':queryData.booking_id,
                    'user_id' :queryData.user_id,
                    'patient_id':queryData.patient_id,
                    'doctor_id':queryData.doctor_id,
                    'rel_id':queryData.rel_id,
                    answer:[]
                }
                var _value='nursing_value'
                if(isrq2){
                    _upDatas = {
                        'booking_id':queryData.booking_id,
                        'user_id' :queryData.user_id,
                        'patient_id':queryData.patient_id,
                        'doctor_id':queryData.doctor_id,
                        'treatment_id':queryData.treatment_id,
                        answer:[]
                    }
                    _value='disease_score'
                }
                var _tempData = this.info
                for(var i =0;i<_tempData.length;i++){
                    var _answer = {
                        type_id:_tempData[i].id
                    }
                    // var _q = _tempData[i].qlist
                    var _selQ = _tempData[i].qlist.filter(function(val){
                        return val.sel == 'true'
                    })
                    if(_selQ.length == 1){
                        _answer['nursing_id'] = _selQ[0].id                       
                        _answer['nursing_name'] = _selQ[0].txt                       
                        _answer[_value] = _selQ[0][_value]                       
                        _upDatas.answer.push(_answer)
                    }else{
                        popShow({
                            txt:'您还有没选中的题目！'
                        })
                        return false
                    }
                    
                }
                //console.log(_upDatas)
                // return false;
                $.post(url.base+url.post,_upDatas,function(res){
                    alert(res.info)
                    popShow({
                        txt:res.info
                    })
                },'json')
            },
        },
        beforeCreate:function(){
            var that= this
            $.get(url.base+url.getCnt,{},function(res){
                console.log(res)
                that.info = res
            },'json')
        },
        created:function(){

        }
    })
}

function popShow(_opts){
    $('.mark').addClass('active');
    $('.pop').addClass('active')
    var defaults={
        oktxt:'确定',
        canceltxt:'取消',
        cancel:false,
        ok:true,
        txt:'<p>文本主体</p>',
        timeable:false,
        time:3000
    }
    var opts=$.extend({},defaults,_opts)
    $('.popcnt').html(opts.txt)
    if(opts.cancel == true){
        $('.pop .popbtn.cancel').show()
    }else{
        $('.pop .popbtn.cancel').hide()
    }
    if((typeof opts.cancel == 'function' || opts.cancel == true) && (typeof opts.ok == 'function' || opts.ok == true)){
        $('.pop .popbtn').removeClass('cols12').addClass('cols6')
    }else{
        $('.pop .popbtn').removeClass('cols6').addClass('cols12')
    }
    $('.popcancel').text(opts.canceltxt)
    $('.popgo').text(opts.oktxt)
    if(typeof opts.cancel == 'function' || opts.cancel == true){
        $('.pop .popcancel').off('click').on('click',function(){
            if(typeof opts.cancel == 'function'){
                opts.cancel()
            }
            $('.mark').removeClass('active');
            $('.pop').removeClass('active')
        })
    }
    if(typeof opts.ok == 'function' || opts.ok == true){
        $('.pop .popgo').off('click').on('click',function(){
            if(typeof opts.ok == 'function'){
                opts.ok()
            }
            $('.mark').removeClass('active');
            $('.pop').removeClass('active');
        })
    }
    if(opts.timeable){
        window.setTimeout(function(){
            $('.pop .popgo').trigger('click')
        },opts.time)
    }
}